package com.kgc.web;

import com.kgc.dao.AdminBulidingDao;
import com.kgc.dao.AdminDanYuanDao;
import com.kgc.entity.Building;
import com.kgc.entity.Danyuan;
import com.kgc.untils.PageUtils;
import com.kgc.vo.R;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "DanyuanServlet", value = "/DanyuanServlet")
public class DanyuanServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String opt = request.getParameter("opt");
        AdminDanYuanDao adminDanYuanDao = new AdminDanYuanDao();
        PrintWriter writer = response.getWriter();
        HttpSession session = request.getSession();
        PageUtils pageUtils = new PageUtils();
        if("list".equals(opt)) {
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            String name = request.getParameter("name");
            int totalDanYuan = adminDanYuanDao.totalDanYuan(name);
            if (page != null) {
                pageUtils.setCurrPageNo(Integer.parseInt(page));
            }
            if (limit != null) {
                pageUtils.setPageSize(Integer.parseInt(limit));
            }
            List<Danyuan> danYuanList = adminDanYuanDao.selectDanYuan(pageUtils, name);
            pageUtils.init(totalDanYuan);
            writer.write(R.success("数据加载成功", totalDanYuan, danYuanList).toJSON());
        } else if("buildinglist".equals(opt)){
            AdminBulidingDao adminBulidingDao = new AdminBulidingDao();
            List<Building> list = adminBulidingDao.selectBuildingName();
            writer.write(R.success("数据加载成功",list).toJSON());
        }else if("add".equals(opt)){
            String name = request.getParameter("name");
            String type = request.getParameter("type");
            int i = adminDanYuanDao.addDanyuan(new Danyuan(name, Integer.parseInt(type)));
            if (i > 0) {
                writer.write(R.success("添加成功!").toJSON());
            } else {
                writer.write(R.error("添加失败!").toJSON());
            }
        }else if ("delete".equals(opt)) {
            String id = request.getParameter("id");
            int count = adminDanYuanDao.deleteBuilding(id);
            if (count > 0) {
                writer.write(R.success("删除成功!").toJSON());
            } else {
                writer.write(R.error("删除失败,数据正在使用中...").toJSON());
            }
        }else if ("edit".equals(opt)) {
            String id = request.getParameter("id");
            String name = request.getParameter("name");
            String buliding = request.getParameter("buliding");
            int count = adminDanYuanDao.updatedanyuan(new Danyuan(Integer.parseInt(id), name, buliding));
            if (count > 0) {
                writer.write(R.success("修改成功!").toJSON());
            } else {
                writer.write(R.error("修改失败").toJSON());
            }
        }else if ("danyuan".equals(opt)) {
            List<Danyuan> danyuanList = adminDanYuanDao.selectDanyuanRoom();
            writer.write(R.success("单元列表加载成功..", danyuanList).toJSON());
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
